 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Sesiones;

import Librerias.Usuario;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

@ManagedBean(name = "SesionUsuario")
@SessionScoped
public class SesionUsuario implements Serializable{
    public void iniciarSesion(Usuario  usr){
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession)context.getSession(true);
        session.setAttribute("Usuario",usr);
    }
    
    public String getUsuarioValidado(){
        String mensaje;
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession)context.getSession(true);
        if(session.getAttribute("Usuario") == null){
            mensaje="INVITADO";
        }else{
            Usuario usr = (Usuario)session.getAttribute("Usuario");
            mensaje = usr.getNombre();
        }
        return mensaje;
    }
    
    public Usuario getUsuario(){
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession)context.getSession(true);
        return (Usuario) session.getAttribute("Usuario");
    }
    
    public void cerrarSesion(){
        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext externalContext = context.getExternalContext();
        Object session = externalContext.getSession(false);
        HttpSession httpSession = (HttpSession) session;
        httpSession.invalidate();
        
    }
    
}
